package com.mofang.ticketing.common.core.enums.base;

import cn.hutool.core.util.StrUtil;
import com.mofang.ticketing.common.core.enums.EnumVO;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 附件类型;1.图片、2音频、3视频、4其它
 *
 * @author LGC
 * @date 2021/7/30 15:26
 * @copyright 2021 mofang. All rights reserved
 */
@Getter
@AllArgsConstructor
public enum AttachmentTypeEnum {
    /**
     * 附件类型;1.图片、2音频、3视频、4其它
     */
    IMG(1, "图片"),
    AUDIO(2, "音频"),
    VIDEO(3, "视频"),
    OTHER(4, "其它"),
    ;

    private final Integer key;
    private final String value;

    public static String getValueByKey(int key) {
        Optional<AttachmentTypeEnum> optional = Arrays.stream(AttachmentTypeEnum.values())
                .filter(v -> v.getKey() == key).findAny();
        return optional.map(AttachmentTypeEnum::getValue).orElse(null);
    }

    public static List<EnumVO> getKeyValueList() {
        return Arrays.stream(AttachmentTypeEnum.values()).map(s ->
                new EnumVO(s.getKey(), s.getValue())).collect(Collectors.toList());
    }

    public static AttachmentTypeEnum getAttachmentType(String fileName) {
        String fileExtension = fileName.substring(fileName.lastIndexOf("."));
        if(StrUtil.isNotBlank(fileExtension)){
            fileExtension = fileExtension.toLowerCase(Locale.ROOT);
            if (imgExtension.contains(fileExtension)) {
                return IMG;
            }
            if (audioExtension.contains(fileExtension)) {
                return AUDIO;
            }
            if (videoExtension.contains(fileExtension)) {
                return VIDEO;
            }
        }
        return OTHER;
    }

    private static final List<String> imgExtension = new ArrayList<>();
    private static final List<String> audioExtension = new ArrayList<>();
    private static final List<String> videoExtension = new ArrayList<>();

    static {

        imgExtension.add(".bmp");
        imgExtension.add(".jpg");
        imgExtension.add(".png");
        imgExtension.add(".jpeg");
        imgExtension.add(".gif");

        audioExtension.add(".mp3");
        audioExtension.add(".wma");
        audioExtension.add(".rm");
        audioExtension.add(".wav");
        audioExtension.add(".mid");
        audioExtension.add(".ape");
        audioExtension.add(".flac");


        videoExtension.add(".mp4");
        videoExtension.add(".m4v");
        videoExtension.add(".mov");
        videoExtension.add(".qt");
        videoExtension.add(".avi");
        videoExtension.add(".flv");
        videoExtension.add(".wmv");
        videoExtension.add(".asf");
        videoExtension.add(".mpeg");
        videoExtension.add(".mpg");
        videoExtension.add(".vob");
        videoExtension.add(".mkv");
        videoExtension.add(".asf");
        videoExtension.add(".rm");
        videoExtension.add(".rmvb");
        videoExtension.add(".vob");
        videoExtension.add(".ts");
        videoExtension.add(".dat");

    }


}
